package kingAI.core;

import java.util.Random;
import java.io.PrintStream;

/**
 * Defines the game.
 * Includes players and runs the game.
 * 
 * @author Equilibrium
 * @version 1.0
 */
public class Game {
	private PrintStream out;
	
	/**
	 * Players enumerating from the owner of 2 of diamonds going counter-clockwise.
	 */
	public Player[] players;
	
	private void correctPlayers(){
		if(players[1].getHand().diamonds[2]){
			Player temp = players[0];
			players[0]=players[1];
			players[1]=players[2];
			players[2]=players[3];
			players[3]=temp;
		}
		else if(players[2].getHand().diamonds[2]){
			Player temp = players[2];
			players[2] = players[0];
			players[0] = temp;
			temp = players[3];
			players[3] = players[1];
			players[1] = temp;
		}
		else if(players[3].getHand().diamonds[2]){
			Player temp = players[3];
			players[3] = players[2];
			players[2] = players[1];
			players[1] = players[0];
			players[0] = temp;
		}
	}
	
	private void swapCards(Card[] cards, int index1 ,int index2){
		Card tempCard=new Card(cards[index1].suit,cards[index1].rank);
		cards[index1].suit=cards[index2].suit;
		cards[index1].rank=cards[index2].rank;
		cards[index2].suit=tempCard.suit;
		cards[index2].rank=tempCard.rank;
	}
	
	private void dealCards(){
		for(int i=0;i<4;i++)
			players[i].flushHand();
		
		//populate an allCards array
		Card[] allCards = new Card[52];
		for(int i=0;i<4;i++)
			for(int j=2;j<15;j++)
				allCards[i*13+j-2]=new Card(Suit.values()[i],j);
		
		//give random cards to everyone
		int lastIndex=51;
		int randIndex;
		Random rand = new Random();
		for(int i=0;i<13;i++)
			for(int j=0;j<4;j++){
				randIndex=rand.nextInt(lastIndex+1);
				players[j].giveCard(allCards[randIndex]);
				swapCards(allCards,randIndex,lastIndex);
				lastIndex--;
			}
	}
	
	/**
	 * Constructs a game all with AIPlayers.
	 
	 * @param out The PrintStream to use when some information is output.
	 */
	public Game(PrintStream out){
		this(true,true,true,true,out);
	}
	
	/**
	 * Constructs a game with given player configuration.
	 * 
	 * @param northIsAI True if north is an AIPlayer.
	 * @param eastIsAI True if east is an AIPlayer.
	 * @param southIsAI True if south is an AIPlayer.
	 * @param westIsAI True if west is an AIPlayer.
	 * @param out The PrintStream to use when some information is output.
	 */
	public Game(boolean northIsAI, boolean eastIsAI, boolean southIsAI, boolean westIsAI, PrintStream out){
		//TODO: Complete game constructor when human player is done.
		this.out = out;
		players = new Player[4];
		players[0]=new AIPlayer("North",this.out);
		players[1]=new AIPlayer("West",this.out);
		players[2]=new AIPlayer("South",this.out);
		players[3]=new AIPlayer("East",this.out);
	}
	
	/**
	 * Starts the game.
	 * 
	 * Deals cards and arranges players according to the possession of 2 of diamonds, then plays the first contract.
	 * Then deals cards and plays a contract 19 more times. 
	 */
	public void startGame(){
		dealCards();
		correctPlayers();
		
		//TODO:play the first contract
		
		/*
		for(int i=0;i<19;i++){
			dealCards();
			//TODO: Play the remaining 19 contracts.
		}
		*/
	}
}
